﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Prisa.BussinesLogic;
using Prisa.Entities;


namespace Prisa.UI
{
    public partial class frmCliente : Form
    {
        [Dependency]
        public IClienteBL cliBL { get; set; }

        public frmCliente()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void frmMttoCliente_Load(object sender, EventArgs e)
        {
            cLIENTEBindingSource.DataSource = cliBL.GetAllFromCliente();
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            cLIENTEBindingSource.DataSource = cliBL.GetFromClienteByCriterio(razonSocialTextBox.Text);
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            frmMttoCliente frmCliente = new frmMttoCliente();
            frmCliente.AddCliente();
            frmCliente.ShowDialog();
            cLIENTEBindingSource.DataSource = cliBL.GetAllFromCliente();
        }   

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            CLIENTE currentCliente = cLIENTEBindingSource.Current as CLIENTE;
            if (currentCliente != null)
            {
                var clienteDef = cliBL.GetFromOneClienteByDniRuc("00000000");
                if (clienteDef != null && clienteDef.Id != currentCliente.Id)
                {
                    frmMttoCliente frmCliente = new frmMttoCliente();
                    frmCliente.CurrentCliente = currentCliente;
                    frmCliente.UpdateCliente();
                    frmCliente.ShowDialog();
                    cLIENTEBindingSource.DataSource = cliBL.GetAllFromCliente();
                }
                else MessageBox.Show("No se puede modificar los datos del cliente por defecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnRemove_Click(object sender, EventArgs e)
        {
            var cliente = cLIENTEBindingSource.Current as CLIENTE;
            var msg = "ERROR";
            try
            {
                if (cliente != null)
                {
                    var clienteDef = cliBL.GetFromOneClienteByDniRuc("00000000");
                    if (clienteDef != null && clienteDef.Id != cliente.Id)
                    {
                        var rpt = MessageBox.Show(this, string.Format("¿Desea eliminar el Cliente: {0}?", cliente.NombreRazonSocial), "Mensaje", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (rpt == DialogResult.Yes)
                        {
                            cliBL.DeleteCliente(cliente.Id);
                            cLIENTEBindingSource.DataSource = cliBL.GetAllFromCliente();
                        }

                    }
                    else
                    {
                        MessageBox.Show("No se puede eliminar el cliente por defecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Debe seleccionar un Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                MessageBox.Show(msg + cliente.NombreRazonSocial+ "\n ya que tiene Créditos-Ventas asociados", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void razonSocialTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cLIENTEBindingSource.DataSource = cliBL.GetFromClienteByCriterio(razonSocialTextBox.Text);
            }

        }

        public CLIENTE CurrentCliente { get; set; }

        public bool Clien { get;  set; }

        private void cLIENTEDataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (Clien == true)
            {
                CurrentCliente = cLIENTEBindingSource.Current as CLIENTE;
                Close();
            }
        }


    }
}
